var score_data = new Object()
function loadScore(type) {
    if (type == "default") return
    $.ajax({
        url: "/score/admin/api/get",
        type: "GET",
        data: {
            type:type
        },
        async: false,
        success : function(data) {
            if (data == "null" || data == "" || data == null || data == undefined) {
                error_bar("考试不存在",function(){location.href='/score/admin'})
                return
            } else {
                for(let i in data) {
                    if (data[i].uid == 0) continue
                    s = JSON.parse(data[i].score)
                    score_data[s.uid] = s
                }
            }
        }
    })
}
loadScore(type)

function jumpNext(e) {
    if (event.keyCode == 13) {
        console.log(nowID + "_" + nowI)
        document.getElementById(nowID + "_" + nowI).focus()
    }
}

function saveScore() {
    input_type = document.getElementById("type_input").value
    console.log(input_type)
    if (input_type == "default") {
        error_bar("default 为系统保留字，不可创建")
        return
    }
    ask_bar("是否上传"+input_type,function() {
        post = new Object()
        for (i=0; i<USER_SUM; i++) {
            uid=user_to_uid[i]
            post[uid] = new Object()
            post[uid]["uid"] = uid
            for(let jj in score_list) {
                j=score_list[jj]
                post[uid][j] = document.getElementById("INPUT_"+uid+"_"+j).value
            }
        }
        $.post("/score/admin/api/post",{
            data:JSON.stringify(post),
            type:input_type
        },function(data) {
            if (data.code != 0) {
                error_bar(data.msg)
                return
            } else {
                success_bar("更新成功，若要撤回请找管理员")
            }
        })
    })
   
}

function createScore(type) {
    input_type = document.getElementById("type_input").value
    if (input_type == "default") {
        error_bar("default 为系统保留字，不可创建")
        return
    }
    ask_bar("是否新建"+input_type,function() {
        $.post("/score/admin/api/create",{
            type:input_type
        },function(data) {
            if (data.code != 0) {
                error_bar(data.msg)
                return
            } else {
                success_bar("更新成功，若要撤回请找管理员")
                location.href = "/score/admin/"+input_type
            }
        })
    })
}
score_list = ["write","listen1","listen2","read1","read2","read3","trans"]
function calcSum(id) {
    sum = 0
    for (i=0; i<7; i++) {
        v = document.getElementById("INPUT_"+id+"_"+score_list[i]).value
        if (v == "" || v==undefined) v=0
        sum += parseFloat(v)
    }
    document.getElementById("SUM_"+id+"_0").innerHTML = sum
    document.getElementById("SUM_"+id+"_1").innerHTML = (sum*7.1).toFixed(2)
}

function sortRank(n) {
    if (n == 0) return;
    ss = score_list[n-1]
    if (n >= 8) sss = "SUM_UID_"+(n-8)
    else sss = "INPUT_UID_"+ss
    list = new Array()
    for (i=0; i<USER_SUM; i++) {
        uid=user_to_uid[i]
        v = document.getElementById(sss.replaceAll("UID",uid)).value
        if (n >= 8)v = document.getElementById(sss.replaceAll("UID",uid)).innerHTML
        if (v == "" || v==undefined) v=0
        list[uid] = {uid:uid,v:v}
    }
    list.sort(function(a,b){
        return b.v - a.v;
    })
    data = new Object()
    for (i=0; i<USER_SUM; i++) {
        data[i] = sysuser[list[i].uid]
    }
    data.length = USER_SUM
    _userloadfunc(data)
}